package tw.com.diray;

import java.util.Calendar;
import java.util.TimeZone;

import tw.com.diary.ae.com.Msg;
import tw.com.diary.api.com.Contract;
import tw.com.diray.comm.ui.DirToast;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.util.Log;

public class DirReceiver extends BroadcastReceiver {
	private static final String TAG = DirReceiver.class.getCanonicalName();

	public DirReceiver() {
	}

	@SuppressLint("NewApi")
	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(Contract.Action.RECEICE_ACTION)) {

			NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

			Notification.Builder bulder = new Notification.Builder(context);

			Intent writeIent = new Intent(context, CalendarWriteActivity.class);

			TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

			stackBuilder.addParentStack(CalendarWriteActivity.class);

			stackBuilder.addNextIntent(writeIent);

			PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

			bulder.setContentIntent(resultPendingIntent);

			Notification notification = bulder//
					.setContentTitle(context.getString(R.string.app_name))//
					.setContentText(context.getString(R.string.wirteTime))//
					.setSmallIcon(R.drawable.ic_action_star)//
					.build();//

			nm.notify(null, 001, notification);
			DirToast.show(context, Msg.S0001, "onReceivce");
		} else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {// 開機要註冊提醒時間

			final SharedPreferences sp = context.getSharedPreferences(Contract.USER_SETTING, 0);

			boolean isAlertFlag = sp.getBoolean(Contract.IS_ALERT_FLAG, false);//開機後會取得是否要提醒資訊

			if (isAlertFlag) {

				final String hour = sp.getString(Contract.ALERT_HOUR, Contract.BLANK);
				final String minute = sp.getString(Contract.ALERT_MINUTE, Contract.BLANK);

				final long systemTime = System.currentTimeMillis();

				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(System.currentTimeMillis());
				calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));

				calendar.add(Calendar.HOUR, Integer.parseInt(hour));
				calendar.add(Calendar.MINUTE, Integer.parseInt(minute));

				long selectTime = calendar.getTimeInMillis();

				if (systemTime > selectTime) {
					Log.v(TAG, "系統時間大於發生時間，時間多家一天,才會觸發");
					calendar.add(Calendar.DAY_OF_MONTH, 1);// 多加一天，表示明天再提醒
					selectTime = calendar.getTimeInMillis();
				}

				// 計算
				final long time = selectTime - systemTime;
				final Long firstTime = time + SystemClock.elapsedRealtime();// 開機時間＋差異時間＝＝提醒時間

				// =設定廣播
				Intent it = new Intent();

				it.setAction("tw.com.receiver");
				it.addCategory(Intent.CATEGORY_DEFAULT);

				PendingIntent pendReciver = PendingIntent.getBroadcast(context, 01, it, PendingIntent.FLAG_ONE_SHOT);

				AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

				am.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 24 * 60 * 60 * 1000, pendReciver);
			}

		}
	}
}
